$(function(){
    function makeDeal(travel_date){
        var $this = $(".fc-day[data-date='"+travel_date+"']");
        if($this.hasClass('button-disable')){
            alert('抱歉,这一天不能选择,请换个日子');
        }else{
            var params = '';
            if(BT.context('hotelId')){
                var data = JSON.parse(localStorage.getItem(BT.context('storageName')));
                data[BT.context('type')] = travel_date;
                localStorage[BT.context('storageName')] = JSON.stringify(data);
            }
            var _search = location.search.replace(/\?/,'').split('&'), _array=[];
            _search.filter(function(i){
                var val = i.split('=');
                if(val[0] == 'type' || val[0] == 'checkin_date' || val[0] == 'checkout_date'){

                }else{
                    if(val[0] == BT.context('type')) val[1] = travel_date;
                    _array.push(val[0]+'=' + val[1]);
                }
                
            });
            var date = '';
            if(BT.context('type') == 'checkin_date'){
                if(travel_date < BT.context('checkout_date')){
                    date = '&checkin_date=' + travel_date + '&checkout_date=' + BT.context('checkout_date');
                }else{
                    var nextDay = moment(travel_date).add(1, 'days').format('l');
                    date = '&checkin_date=' + travel_date + '&checkout_date=' + nextDay;
                }
            }else{
                date = '&checkout_date=' + travel_date + '&checkin_date=' + BT.context('checkin_date');
            }
            location.href = location.origin + BT.context('backUrl') + '?' + _array.join('&') + date;
        }
    }
    var checkin_date = BT.context('checkin_date');
    var url = location.origin + "/hotel/book/"+ BT.context('hotelId') + "/date_api/?type="+BT.context('type')+'&checkin_date=' + BT.context('checkin_date') + '&checkout_date='+BT.context('checkout_date');
    var configs = {
        header: {
            left: 'prev',
            center: 'title',
            right: 'next'
        },
        titleFormat: {
            month: 'yyyy-MM'
        },
        editable: false,
        selectable: true,
        selectHelper: false,
        monthNames: ["1月", "2月", "3月", "4月","5月","6月","7月","8月","9月","10月","11月","12月"],
        monthNamesShort: ["1月", "2月", "3月", "4月","5月","6月","7月","8月","9月","10月","11月","12月"],
        dayNamesShort: ["日", "一", "二", "三", "四", "五", "六"],
        select: function(start, end, allDay){
            var travel_date = $.fullCalendar.formatDate(start, 'yyyy-MM-dd');
            makeDeal(travel_date);
        },
        events: {
            url: url,
            textColor: "#fc5d7b"
        },
        eventRender: function(event, element) {
            var date = event.title;
            $(".fc-day[data-date='"+date+"']").addClass('button-disable');
            element.find('.fc-event-title').hide();
        },
    }
    var date = BT.context('"' + BT.context('type') + '"');
    configs.year = moment(date).year();
    configs.month = moment(date).month();
    
    window.calendar = $("#calendar").fullCalendar(configs);
});